package cn.buk.tms.eotms.util;

import cn.buk.tms.eotms.dto.VasOrderDto;
import cn.buk.tms.eotms.entity.*;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yfdai
 */
public class FlightOrderUtil {

    public static void clean(FlightOrder order) {
        if (order == null) {
            return;
        }

        for (FlightOrderFlight flt : order.getFlights()) {
            flt.setOrder(null);
        }

        for (FlightOrderPassenger psg : order.getPassengers()) {
            psg.setOrder(null);
        }

        order.setOperator(null);

    }

    public static void cleanFlightRefundOrder(FlightRefundOrder order) {
        if (order == null) {
            return;
        }

        for(FlightRefundOrderDetail detail: order.getFlights()) {
            detail.setOrder(null);
        }
    }

    public static void cleanFlightChangeOrder(FlightChangeOrder order) {
        if (order == null) {
            return;
        }

        for (FlightChangeOrderDetail detail : order.getFlights()) {
            detail.setOrder(null);
        }
    }

    public static List<VasOrderDto> convertToVasOrderDto(List<VasOrder> vasOrders) {
        cleanVasOrders(vasOrders);

        List<VasOrderDto> dtos = new ArrayList<>();
        for(VasOrder order: vasOrders) {
            VasOrderDto dto = VasOrderDto.createByVasOrder(order);

            dtos.add(dto);
        }

        return dtos;
    }

    public static String getItineraryTypeDesc(int type) {
        switch (type) {
            case 1:
                return "行程单";
            case 2:
                return "发票";
            case 3:
                return "信息单";
            default:
                return "";
        }
    }

    private static void cleanVasOrders(List<VasOrder> orders) {
        for(VasOrder vasOrder: orders) {
            cleanVasOrder(vasOrder);
        }
    }

    private static void cleanVasOrder(VasOrder vasOrder) {
        for(VasOrderPassenger psg: vasOrder.getPassengers()) {
            psg.setOrder(null);
        }

        for(VasOrderFlight flt: vasOrder.getFlights()) {
            flt.setOrder(null);
        }
    }

    /**
     * 校验航班时间是否符合要求
     */
    public static boolean validateFlightTime(final String dtime) {
        if (dtime == null) {
            return false;
        }
        final String regEx = "^[0-2][0-9]{3}([+]1)?$";
        final Pattern pattern = Pattern.compile(regEx);

            Matcher m = pattern.matcher(dtime);
            if (m.find()) {
                return true;
            }

        return false;
    }

//    public static List<VasOrderDto> convertToVasOrderDto(List<VasOrder> orders) {
//        cleanVasOrders(orders);
//
//        List<VasOrderDto> dtos = new ArrayList<>();
//        for(VasOrder order: orders) {
//            VasOrderDto dto = VasOrderDto.createByVasOrder(order);
//            searchVasOrderFlights(dto);
//
//            dtos.add(dto);
//        }
//
//        return dtos;
//    }

}
